package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.mapper.EmployeeMapper;
import cn.wolfcode.crm.qo.EmployeeQuery;
import cn.wolfcode.crm.service.IEmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Service
public class EmployeeServiceimpl implements IEmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public void save(Employee employee, Long[] ids) {
        //进行密码加密处理
        String password = employee.getPassword();
        //加盐
        Md5Hash hash = new Md5Hash(password, employee.getName());
        employee.setPassword(hash.toString());
        employeeMapper.insert(employee);
        //处理中间表数据
        if (ids != null && ids.length > 0) {
            employeeMapper.deleteRelation(employee.getId());
            for (Long rid : ids) {
                employeeMapper.insertEmpByRole(employee.getId(),rid);
            }
        }
    }

    @Override
    public void deleteByPrimaryKey(Long id) {

        employeeMapper.deleteRelation(id);
        employeeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void updateByPrimaryKey(Employee employee,Long[] ids) {

        employeeMapper.updateByPrimaryKey(employee);
        //处理中间表数据
        if (ids != null && ids.length > 0) {
            employeeMapper.deleteRelation(employee.getId());
            for (Long rid : ids) {
                employeeMapper.insertEmpByRole(employee.getId(),rid);
            }
        }
    }

    @Override
    public Employee get(Long id) {
        return employeeMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Employee> selectAll() {
        return employeeMapper.selectAll();
    }

    @Override
    public PageInfo query(EmployeeQuery qo) {
        /*int count = employeeMapper.queryForCount(qo);
        if (count==0) {
            return new PageResult<Employee>(qo.getPageSize());
        }*/
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Employee> list = employeeMapper.queryForList(qo);
        return new PageInfo(list);

    }

    @Override
    public Employee login(String username, String password) {
        Employee employee = employeeMapper.login(username, password);
        //判断用户是否存在
        if (employee == null) {
            throw new RuntimeException("账号或密码错误,请重新输入!");
        }
        return employee;
    }

    @Override
    public void deleteBatch(Long[] ids) {
        //删除中间表
        employeeMapper.deleteBatchRelation(ids);
        employeeMapper.deleteBatch(ids);
    }

    @Override
    public Employee checkName(String name) {
        return employeeMapper.checkName(name);
    }

    @Override
    public List<Employee> selectByclassAll() {
        List<Employee> employees = employeeMapper.selectByclassAll();
        return employees;
    }

    /**
     *文件导出
     * @return
     */
    @Override
    public Workbook exportXls() {
        //创建Excel
        Workbook wb = new HSSFWorkbook();
        //创建一页纸
        Sheet sheet = wb.createSheet("员工名单");
        //标题行
        Row row = sheet.createRow(0);
        //设置内容到单元格
        row.createCell(0).setCellValue("姓名");
        row.createCell(1).setCellValue("邮箱");
        row.createCell(2).setCellValue("年龄");
        //获得所有员工信息
        List<Employee> employees = employeeMapper.selectAll();
        for (int i = 0; i < employees.size(); i++) {
            Employee employee = employees.get(i);
            //创建行,一个员工一行
            row = sheet.createRow(i+1);
            //设置内容到单元格
            row.createCell(0).setCellValue(employee.getName());
            row.createCell(1).setCellValue(employee.getEmail());
            row.createCell(2).setCellValue(employee.getAge());
        }
        return wb;
    }

    /**
     * 文件导入
     * @param file
     */
    @Override
    public void importXls(MultipartFile file) throws IOException {
        //把接收到的参数转换成Excel文件
        HSSFWorkbook xwb = new HSSFWorkbook(file.getInputStream());
        //取第一页的数据
        Sheet sheet = xwb.getSheetAt(0);
        //获得最后一条数据的索引(+1为总行数)
        int lastRowNum = sheet.getLastRowNum();
        //把第一页的数据进行遍历
        for (int i = 1; i <= lastRowNum; i++) {//忽略标题
            //获得每一行(每一行其实就是一个对象)
            Row row = sheet.getRow(i);
            //创建对象把数据存进对象
            Employee employee = new Employee();
            employee.setName(row.getCell(0).getStringCellValue());
            employee.setEmail(row.getCell(1).getStringCellValue());
            employee.setAge(Integer.valueOf(row.getCell(2).getStringCellValue()));
            //设置默认密码1
            employee.setPassword("1");
            this.save(employee,null);
        }
    }

    @Override
    public List<Employee> selectEmpByRole(String... sns) {
        List<Employee> employees = employeeMapper.selectEmpByRole(sns);
        return employees;
    }

}
